<?php

namespace App\Http\Controllers;

// 用户模型
use App\Models\Business\Business as BusinessModel;
use App\Models\Business\Address as AddressModel;
use App\Models\Region as RegionModel;

// 引入Validator 门面 
use Illuminate\Support\Facades\Validator;


class Address extends Controller
{
    // 添加
    public function add()
    {
        $busid = request('busid', 0);
        $params = request()->all();
        $code = $params['code'] ?? '';

        $businessinfo = BusinessModel::find($busid);

        if (!$businessinfo) {
            return error('用户不存在');
        }

        $data = [
            'busid' => $busid,
            'consignee' => $params['consignee'],
            'address' => $params['address'],
            'mobile' => $params['mobile'],
            'status' => $params['status'],
        ];

        // 判断地区
        $parentpath = RegionModel::where('code', $code)->value('parentpath');

        if (!$parentpath) {
            return error('地址不存在');
        }

        $path = explode(',', $parentpath);

        $data['province'] = $path[0] ??  '';
        $data['city'] = $path[1] ??  '';
        $data['district'] = $path[2] ??  '';

        // 判断是否添加默认地址
        if (isset($params['status']) && $params['status'] == '1') {
            // 获取一列的值
            $ids = AddressModel::where('busid', $busid)->pluck('id');

            $addressstatus = AddressModel::whereIn('id', $ids)->update(['status' => '0']);

            if ($addressstatus === false) {
                return error('更新默认地址失败');
            }
        }

        // 校验器
        $result = AddressModel::create($data);

        if ($result === false) {
            return error('添加失败');
        } else {
            return success('添加成功');
        }
    }

    // 列表
    public function index()
    {
        $busid = request('busid', 0);

        $businessinfo = BusinessModel::find($busid);

        if (!$businessinfo) {
            return error('用户不存在');
        }

        $data = AddressModel::Where('busid', $busid)->orderBy('id', 'desc')->get();

        return success('', $data);
    }

    //详情
    public function info()
    {
        $busid = request('busid', 0);
        $addressid = request('addressid', 0);

        $businessinfo = BusinessModel::find($busid);

        if (!$businessinfo) {
            return error('用户不存在');
        }

        $addressinfo = AddressModel::find($addressid);

        if (!$addressinfo) {
            return error('地址不存在');
        }

        return success('获取成功', $addressinfo);
    }

    //编辑
    public function edit()
    {
        $busid = request('busid', 0);
        $addressid = request('addressid', 0);
        $params = request()->all();
        $code = $params['code'] ?? '';

        $businessinfo = BusinessModel::find($busid);

        if (!$businessinfo) {
            return error('用户不存在');
        }

        $addressinfo = AddressModel::find($addressid);

        if (!$addressinfo) {
            return error('收货地址不存在');
        }

        $data = [
            'busid' => $busid,
            'consignee' => $params['consignee'],
            'address' => $params['address'],
            'mobile' => $params['mobile'],
            'status' => $params['status'],
        ];

        // 判断地区
        $parentpath = RegionModel::where('code', $code)->value('parentpath');

        if (!$parentpath) {
            return error('地址不存在');
        }

        $path = explode(',', $parentpath);

        $data['province'] = $path[0] ??  '';
        $data['city'] = $path[1] ??  '';
        $data['district'] = $path[2] ??  '';

        // 判断是否添加默认地址
        if (isset($params['status']) && $params['status'] == '1') {
            // 获取一列的值
            $ids = AddressModel::where('busid', $busid)->pluck('id');

            $addressstatus = AddressModel::whereIn('id', $ids)->update(['status' => '0']);

            if ($addressstatus === false) {
                return error('更新默认地址失败');
            }
        }


        $result = AddressModel::where('id', $addressid)->update($data);

        if ($result === false) {
            return error('编辑失败');
        } else {
            return success('编辑成功');
        }
    }

    //删除
    public function del()
    {
        $busid = request('busid', 0);
        $addressid = request('addressid', 0);

        $businessinfo = BusinessModel::find($busid);

        if (!$businessinfo) {
            return error('用户不存在');
        }

        $addressinfo = AddressModel::find($addressid);

        if (!$addressinfo) {
            return error('收货地址不存在');
        }

        $result = AddressModel::destroy($addressid);

        if ($result === false) {
            return error('删除失败');
        } else {
            return success('删除成功');
        }
    }

    //切换默认
    public function check()
    {
        $busid = request('busid', 0);
        $addressid = request('addressid', 0);

        $businessinfo = BusinessModel::find($busid);

        if (!$businessinfo) {
            return error('用户不存在');
        }

        $addressinfo = AddressModel::find($addressid);

        if (!$addressinfo) {
            return error('收货地址不存在');
        }

        // 获取一列的值
        $ids = AddressModel::where('busid', $busid)->pluck('id');

        $addressstatus = AddressModel::whereIn('id', $ids)->update(['status' => '0']);

        if ($addressstatus === false) {
            return error('更新默认地址失败');
        }

        $result = AddressModel::where('id', $addressid)->update(['status' => '1']);

        if ($result === false) {
            return error('切换失败');
        } else {
            return success('切换成功');
        }
    }

    public function order()
    {
        $result = AddressModel::where(['status' => '1'])->first();

        return success('', $result);
    }
}
